#include <myheader_c_plus.h>
#include <map>
#include <unordered_map>
using std::vector;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::unordered_map;
using std::make_pair;
class Dictionary{
private:
    unordered_map<string, int> dic;
public:
    //构造函数，申请足够的空间
    Dictionary(){
    }
    void read(const string &fileName){
        //打开文件
        ifstream ifs(fileName);
        string word;
        while(ifs >> word){
           // cout << 20 << " " << "word = " << word << endl;
            //判断这个字符是否在map内
            auto search = dic.find(word);
            if(search != dic.end()){
                //说明在dic内
                search->second ++;
            }

            if(search != dic.end()){
                continue;
            }
            //没有找到相同的
            dic.insert(make_pair(word, 1));
            //sleep(1);
            //for(long unsigned i = 0; i < dic.size(); i++){
              //  cout << 40 << "_word = " << dic[i]._word << "_num = " << dic[i]._num << endl;
            //}
          // cout << "==============" << endl;
        }
        ifs.close();
        //sleep(1);

    }
    void write(const string &fileName){
        ofstream ofs(fileName);
        //将dic里的结构体输入到文件
        auto it = dic.begin();
        while(it != dic.end()){
            ofs << it->first << " " << it->second << endl;
            it++;
        }
        ofs.close();

    }
};
int main(int argc, char *argv[]){
    Dictionary dic;
    auto start = std::chrono::high_resolution_clock::now();
    dic.read("The_Holy_Bible.txt");
    dic.write("dict.txt");
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
    cout << "runtime = " << duration.count() << " ms" <<  endl;

    return 0;
}


